# type: ignore
from PySide6.QtCore import *
from PySide6.QtWidgets import *
import time

# 创建一个耗时任务的线程类


class WorkerThread(QThread):
    # 定义一个信号，用于线程完成任务时通知主线程
    finished = Signal(str)

    def run(self):
        # 模拟耗时操作
        time.sleep(5)  # 睡眠5秒钟
        result = "Task completed!"
        # 发射信号通知主线程任务已完成
        self.finished.emit(result)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        # 创建主窗口的中央小部件和布局
        self.central_widget = QWidget()
        self.layout = QVBoxLayout(self.central_widget)

        # 创建 QLabel 显示状态
        self.status_label = QLabel("Ready")
        self.layout.addWidget(self.status_label)

        # 创建启动线程的按钮
        self.start_button = QPushButton("Start Task")
        self.start_button.clicked.connect(self.start_task)
        self.layout.addWidget(self.start_button)

        self.setCentralWidget(self.central_widget)

    def start_task(self):
        # 创建并启动线程
        self.thread = WorkerThread()
        self.thread.finished.connect(self.on_task_finished)
        self.thread.start()

        # 更新界面状态
        # self.status_label.setText("Task in progress...")
        # time.sleep(1)
        # self.status_label.setText("Task in progress2222...")
        # time.sleep(1)
        self.status_label.setText("Task in progress555555...")

    def on_task_finished(self, result):
        # 当线程完成任务时更新界面
        self.status_label.setText(result)


app = QApplication([])
window = MainWindow()
window.show()
app.exec()
